#include "config.h"

#include "ksix.h"
#include "carrier.h"
#include "misc.h"

//#include <kapp.h>
// #include <dcopclient.h>
// #include <kaboutdata.h>
// #include <kcmdlineargs.h>
// #include <klocale.h>
// #include <kmessagebox.h>
#include <qfile.h>
#include <QApplication>
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QDebug>
#include <QTranslator>
#define I18N_NOOP 
#define VERSION "0.53.0"

using std::cin;

static const char *description = I18N_NOOP("Six is a Hex playing program.");

static const char *version = "v" VERSION;

static QList<QCommandLineOption> options =
{
    { "black ", I18N_NOOP("Set black player. Possible values are:\n"
                          "\"human\", \"beginner\", \"intermediate\",\n"
                          "\"advanced\", \"expert\"."), ""},
    { "white ", I18N_NOOP("Set white player. Possible values are:\n"
                          "\"human\", \"beginner\", \"intermediate\",\n"
                          "\"advanced\", \"expert\"."), ""},
    { "pbem-filter", I18N_NOOP("Import PBEM game from standard input."), 0 },
    { "+[FILE]", I18N_NOOP("Start with the game loaded from FILE."), 0 },
    { 0, 0, 0 }
};

int main(int argc, char **argv)
{
  RANDOMIZE();
  Carrier::init();
//   KAboutData about("six", I18N_NOOP("Six"), version, description,
//                    KAboutData::License_GPL, "(C) 2002-2004 Gábor Melis",
//                      0, "http://six.retes.hu/", "mega@hotpop.com");
//   about.addAuthor("Gábor Melis", 0, "mega@hotpop.com");
//   KCmdLineArgs::init(argc, argv, &about);
//  KCmdLineArgs::addCmdLineOptions(options);
  QCommandLineParser* args = new QCommandLineParser();
  args->addOptions(options);
  
  QApplication app(argc,argv);
  args->process(app);
  QTranslator translator;
  translator.load(":/six_zh.qm");
  app.installTranslator(&translator);
  // register ourselves as a dcop client
  //app.dcopClient()->registerAs(app.name(), false);

  // see if we are starting with session management
//   if (app.isRestored()) {
//     //RESTORE(KSix);
//   } else 
  {
    // no session.. just start up normally
    //KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    KSix *widget = new KSix;
    widget->show();
    if(args->isSet("pbem-filter")) {
      widget->importPBEMGame(cin, QObject::tr("standard input"));
    }
    if(args->value("black") != "") {
      KSix::PlayerType black = KSix::stringToPlayer(args->value("black"));
      if(black == KSix::PLAYER_NONE) {
        qDebug()<<(QObject::tr("Invalid value for black player: %1")
                            .arg(args->value("black")));
      } else {
        widget->setBlack(black);
      }
    }
    if(args->value("white") != "") {
      KSix::PlayerType white = KSix::stringToPlayer(args->value("white"));
      if(white == KSix::PLAYER_NONE) {
        qDebug()<<(QObject::tr("Invalid value for white player: %1")
                            .arg(args->value("white")));
      } else {
        widget->setWhite(white);
      }
    }
	QStringList positionalArgs = args->positionalArguments();
    for(int i = 0; i < positionalArgs.count(); i++) {
      widget->loadGame(/*QFile::decodeName*/(positionalArgs[i]));
    }
    //args->clear();
	delete args;
  }

  return app.exec();
}
